import React, { FunctionComponent } from 'react';
import { Form } from 'antd';
import * as API from '@/services';
import { DEFAULT_FORM_FLEX_LAYOUT, KfBizQuickFormProps, KfFloatingContextProps, KfInput, KfQuickForm } from 'kf-web-library';

const FormItem = Form.Item;
type QueryEntity = API.Material;
type ModifyEntity = API.Material_Modify;

type DataFormProps = {} & KfBizQuickFormProps & KfFloatingContextProps;


const QuickForm: FunctionComponent<DataFormProps> = (props) => {

    const {formType, bizId, ...rest} = props;

    return (
        <KfQuickForm
            formType={formType}
            formProps={{
                ...DEFAULT_FORM_FLEX_LAYOUT,
            }}
            defaultQueryRequest={() => API.queryMaterial(bizId!).then(resp => {
                if (resp.flag) {
                    resp.data = {
                        ...resp.data,
                    }
                }
                return new Promise((resolve) => resolve(resp))
            })}
            submitRequest={{
                "modify": (formValues, queryResult) => API.modifyMaterial({...queryResult,...formValues} as ModifyEntity),
            }}
            {...rest}
        >
            <FormItem name={nameof<ModifyEntity>((x) => x.name)} label={"名称"} rules={[{required: true, max: 30}]}>
                <KfInput/>
            </FormItem>
        </KfQuickForm>
    );
};

export default QuickForm;
